
import { ResourcesModel } from "./mode/resources";
import { UserModel } from "./mode/user";


export class AppContext {
  private static _instance: AppContext;
  private _token: string = '';
  private _tabbarActive = 0; // tabbar 底表
  private _user: UserModel;
  private _resources: ResourcesModel;

  constructor() {
    this._user = new UserModel();
    this._resources = new ResourcesModel();
    console.log("AppContext lazy loading singleton has created");
  }

  public static get instance(): AppContext {
    if (!this._instance) {
      this._instance = new AppContext();
    }
    return this._instance;
  }

  /**
   * token
   * @param token 
   */
  public setToken(token: string): void {
    wx.setStorage({ key: "token", data: token });
    this._token = token;
  }
  public getHeader(): any {
    const token = this._token || wx.getStorage({
      key: 'key',
      success(res) {
        return res.data;
      }
    });
    console.log('token:>>>' + token);
    if (token) {
      const header = {
        "Content-Type": "application/json; charset=utf-8",
        token,
      };
      return header;
    }
  }

  public setTabbarActive(tabbarActive: number) {
    this._tabbarActive = tabbarActive;
  }
  public get tabbarActive(): number {
    return this._tabbarActive;
  }

  /** 用户 */
  public get user(): UserModel {
    return this._user;
  }

  /** 资源 */
  public get resources(): ResourcesModel {
    return this._resources;
  }







}